package com.bizfx.component.controls
{
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import com.bizfx.component.authentication.AuthenticationInfo;
	import com.bizfx.component.authentication.ILoginUI;
	import com.bizfx.events.LoginUICancelEvent;
	import com.bizfx.events.LoginUISubmitEvent;
	import com.bizfx.utils.StringUtils;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.TextInput;
	import mx.events.FlexEvent;
	import mx.validators.StringValidator;
	import mx.validators.Validator;
	
	[Event(name="loginSubmit", type="com.bizfx.events.LoginUISubmitEvent")]
	
	[Event(name="loginCancel", type="com.bizfx.events.LoginUICancelEvent")] 	
	
	public class BaseLoginUI extends VBox implements ILoginUI
	{
		private var userNameValidator:StringValidator;
		
		private var passwordValidator:StringValidator;
		
		private var validators:ArrayCollection = new ArrayCollection();
		
		public var userNameInputId:String = "tb_UserName";
		
		public var passwordInputId:String = "tb_Password";
		
		public var submitButtonId:String = "btn_Submit";
		
		public var resetButtonId:String = "btn_Reset";
		
		private var userNameInput:TextInput;
		
		private var passwordInput:TextInput;
		
		private var submitButton:Button;
		
		private var resetButton:Button;
		
		public function get userName(): String
		{
			return StringUtils.trim(this.userNameInput.text);  
		}
		
		public function set userName(value: String):void
		{
			this.userNameInput.text = StringUtils.trim(value);  
		}
		
		public function get password(): String
		{
			return this.passwordInput.text;  
		}
		
		public function set password(value: String):void
		{
			this.passwordInput.text = value;  
		}
		
		public function BaseLoginUI() {
			this.addEventListener(FlexEvent.CREATION_COMPLETE, createCompleteHandler);
		}
		
		public function showUI():void
		{
			this.includeInLayout = true;
			this.visible = true;
		}
		
		public function hideUI():void
		{
			this.includeInLayout = false;
			this.visible = false;
		}
		
		public function reset():void
		{
			this.passwordInput.text = "";
			this.userNameInput.text = "";
		}
		
		public function loginFailed(errorMessage:String):void
		{
			this.passwordInput.text = "";
			this.passwordInput.setFocus();
			Alert.show(errorMessage, "错 误");
		}
		
		private function login():void {
			var error:Array = Validator.validateAll(this.validators.toArray());
			if (error.length != 0) {
				return;
			}
//			container.enabled = false;
			this.dispatchEvent(new LoginUISubmitEvent(new AuthenticationInfo(this.userName, this.password)));
		}
		
		override protected function createChildren():void {
			super.createChildren();
			this.userNameValidator = new StringValidator();
			this.validators.addItem(this.userNameValidator);
			this.passwordValidator = new StringValidator();
			this.validators.addItem(this.passwordValidator);
		}
		
		override protected function childrenCreated():void {
			if (this[submitButtonId]) {
				this.submitButton = this[submitButtonId];
				this.submitButton.addEventListener(MouseEvent.CLICK, submitButton_clickHandler);
				this.submitButton.addEventListener(KeyboardEvent.KEY_UP, submitButton_keyUpHandler);
			}
			
			if (this[resetButtonId]) {
				this.resetButton = this[resetButtonId];
				this.resetButton.addEventListener(MouseEvent.CLICK, resetButton_clickHandler);
			}
			
			if (this[userNameInputId]) {
				this.userNameInput = this[userNameInputId];
				this.userNameInput.addEventListener(KeyboardEvent.KEY_UP, userNameInput_keyUpHandler);
				
				userNameValidator.source = this.userNameInput;
				userNameValidator.property = "text";
				userNameValidator.trigger = this.submitButton;
				userNameValidator.triggerEvent = MouseEvent.CLICK;
				userNameValidator.requiredFieldError = "请输入用户名。";
			}
			
			if (this[passwordInputId]) {
				this.passwordInput = this[passwordInputId];
				this.passwordInput.addEventListener(KeyboardEvent.KEY_UP, submitButton_keyUpHandler);
				
				passwordValidator.source = this.passwordInput;
				passwordValidator.property = "text";
				passwordValidator.trigger = this.submitButton;
				passwordValidator.triggerEvent = MouseEvent.CLICK;
				passwordValidator.requiredFieldError = "请输入密码。";
			}
			
			super.childrenCreated();
		}
		
		private function submitButton_clickHandler(event:MouseEvent):void
		{
			this.login();
		}
			
		private function resetButton_clickHandler(event:MouseEvent):void
		{
			this.passwordInput.text = "";
			this.userNameInput.text = "";
		}
		
		protected function userNameInput_keyUpHandler(event:KeyboardEvent):void {
			switch (event.keyCode) {
				case 13:
					this.passwordInput.setFocus();	
					break;
			}
		}
		
		protected function submitButton_keyUpHandler(event:KeyboardEvent):void {
			if (event.keyCode == 13) {
				this.login();
			}
		}
		
		protected function createCompleteHandler(event:FlexEvent):void
		{
			if (this.userNameInput) {
				this.userNameInput.setFocus();
			}
		}
	}
}